<template>
  <transition name="fade">
    <div v-if="show" :class="['error-message', type]">
      <i :class="iconClass"></i>
      <span>{{ message }}</span>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'ErrorMessage',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    message: {
      type: String,
      default: '发生错误'
    },
    type: {
      type: String,
      default: 'error',
      validator: value => ['error', 'warning', 'success'].includes(value)
    }
  },
  computed: {
    iconClass() {
      const icons = {
        error: 'el-icon-error',
        warning: 'el-icon-warning',
        success: 'el-icon-success'
      }
      return icons[this.type]
    }
  }
}
</script>

<style scoped>
.error-message {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  border-radius: 4px;
  color: #fff;
  display: flex;
  align-items: center;
  z-index: 10000;
}

.error-message.error {
  background-color: #f56c6c;
}

.error-message.warning {
  background-color: #e6a23c;
}

.error-message.success {
  background-color: #67c23a;
}

.error-message i {
  margin-right: 8px;
  font-size: 16px;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity .3s, transform .3s;
}

.fade-enter, .fade-leave-to {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
}
</style>